package org.example.managementsystem.repository;

import org.example.managementsystem.model.ProviderApplicationReview;
import org.example.managementsystem.model.ProviderComplaintReview;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 供应商申请审核记录数据访问对象
 * @author 王振祎
 * @date 2025-05-21
 */
@Repository
public class ProviderApplicationReviewDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public ProviderApplicationReviewDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 插入供应商申请记录
     * @param review 审核记录
     * @return 插入的行数
     */
    public int insertReview(ProviderApplicationReview review) {
        String sql = "INSERT INTO provider_admission_review (application_id, is_approved, description, initial_rating) " +
                "VALUES (?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                review.getApplicationId(),
                review.isApproved(),
                review.getDescription(),
                review.getInitialRating());
    }


    /**
     * 更新供应商申请记录
     * @param review 待更新的审核记录
     * @return 更新的行数
     */
    public int updateReview(ProviderApplicationReview review) {
        String sql = "UPDATE provider_admission_review SET is_approved = ?, description = ?, initial_rating = ? " +
                "WHERE application_id = ?";
        return jdbcTemplate.update(sql,
                review.isApproved(),
                review.getDescription(),
                review.getInitialRating(),
                review.getApplicationId());
    }

    public int updateReview(int applicationId, boolean isApproved, String description, int initialRating) {
        String sql = "UPDATE provider_admission_review SET is_approved = ?, description = ?, initial_rating = ? " +
                "WHERE application_id = ?";
        return jdbcTemplate.update(sql,
                isApproved,
                description,
                initialRating,
                applicationId);
    }

    /**
     * 获取所有供应商申请记录
     * @return 所有供应商申请记录
     */
    public List<ProviderApplicationReview> getAllReviews() {
        String sql = "SELECT * FROM provider_admission_review";
        return jdbcTemplate.query(sql, new ProviderApplicationReviewRowMapper());
    }

    /**
     * 根据申请ID获取供应商申请审核记录
     * @param applicationId
     * @return
     */
    public ProviderApplicationReview getReviewByApplicationId(int applicationId) {
        String sql = "SELECT * FROM provider_admission_review WHERE application_id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new ProviderApplicationReviewRowMapper(), applicationId);
        } catch (EmptyResultDataAccessException e) {
            return null; // 或者返回
        }
    }


    /**
     * 供应商申请审核记录映射器
     */
    private static class ProviderApplicationReviewRowMapper implements RowMapper<ProviderApplicationReview> {
        @Override
        public ProviderApplicationReview mapRow(ResultSet rs, int rowNum) throws SQLException {
            ProviderApplicationReview review = new ProviderApplicationReview();
            review.setApplicationId(rs.getInt("application_id"));
            review.setApproved(rs.getBoolean("is_approved"));
            review.setDescription(rs.getString("description"));
            review.setInitialRating(rs.getInt("initial_rating"));
            return review;
        }
    }

}
